<?php
namespace app\index\controller;

use app\index\model\expert as ExpertModel;
use app\index\model\user as UserModel;
use app\index\model\appKind as KindModel;
use app\index\model\certifyNeed as CertifyNeedModel;
use app\index\model\certifyEnumValue as CertifyEnumValueModel;
use app\index\model\certifyInfoStore as CertifyInfoStoreModel;
use app\index\model\certifyStatus as CertifyStatusModel;

use think\Request;
use think\Db;

class Expert 
{
    //助手函数 是否是专家
    public function isExpert($userToken)
    {
        $user = UserModel::get(["userToken" => $userToken]);
        if ($user == NULL) {
            return false;
        }
        else {
            return $user["isExpert"] ? true : false;
        }
    }
    
    //认证所需的资料
    public function certifyNeed(Request $request)
    {
        //加载所需的基本信息
        $basicNeed = CertifyNeedModel::all(["belong" => "basic"]);
        
        //基本信息需要加上分类一栏
        //获取所有分类信息
        $allNote = KindModel::all();
        $allKindName = [];
        foreach ($allNote as $key => $value) {
            $allKindName[] = $value["kindName"];
        } 

        //加载专家的所需的信息
        $expertNeed = CertifyNeedModel::all(["belong" => 'expert']);

        //加载所需的证件图片信息
        $photoNeed = CertifyNeedModel::all(["belong" => "photo"]);

        //查询 enum 的值类型，并将其添加到 对象上
        foreach ($basicNeed as $key => $value) {
            if ($value["infoType"] == "enum") {
                if ($value["infoName"] != "认证分类") {
                    $allValue = [];
                    $allNote = CertifyEnumValueModel::all(["infoName" => $value["infoName"]]);
                    foreach ($allNote as $k => $v) {
                        $allValue[] = $v["oneValue"];
                    }
                    $basicNeed[$key]["chooseValue"] = $allValue;
                }
                else {
                    $basicNeed[$key]["chooseValue"] = $allKindName;
                }
            }
            
        }

        foreach ($expertNeed as $key => $value) {
            if ($value["infoType"] == "enum") {
                $allValue = [];
                $allNote = CertifyEnumValueModel::all(["infoName" => $value["infoName"]]);
                foreach ($allNote as $k => $v) {
                    $allValue[] = $v["oneValue"];
                }
                $expertNeed[$key]["chooseValue"] = $allValue;
            }
        }

        //定义变量 data 保存上面所需的分类信息
        $data = [];
        $data['basicNeed'] = $basicNeed;
        $data['expertNeed'] = $expertNeed;
        $data['photoNeed'] = $photoNeed;

        //返回消息
        $JsonData = controller("index/JsonData");
        $json = $JsonData->getJsonData(0, "get need input info success.", $data);
        return $json;
             
    }

    public function dealSubmitData($data, $userToken)
    {
        foreach ($data as $key => $value) {
            $data[$key]["userToken"] = $userToken;
            $data[$key]["belong"] = $value["belong"];
            $data[$key]["kindName"] = $value["kindName"];
            $data[$key]["infoName"] = $value["keyName"];
            $data[$key]["infoType"] = $value["type"];
            $data[$key]["infoValue"] = $value["value"];
        }
        return $data;
    }

    //专家提交认证信息 保存到认证信息表 dcw_fastask_certify_info_store
    public function certifyExpert(Request $request)
    {
        //获取请求参数
        $userToken = $request->param("userToken");
        $basicInfo = $request->param("basicInfo");
        $expertInfo = $request->param("expertInfo");
        $photoInfo = $request->param("photoInfo");

        //转json数组
        $basicInfo = json_decode($basicInfo, true);
        $expertInfo = json_decode($expertInfo, true);
        $photoInfo = json_decode($photoInfo, true);


        $basicInfo = $this->dealSubmitData($basicInfo, $userToken);
        $expertInfo = $this->dealSubmitData($expertInfo, $userToken);
        $photoInfo = $this->dealSubmitData($photoInfo, $userToken);

        //初始化消息
        $message = "";

        //查询用户是否有资格保存数据
        $note = CertifyStatusModel::get(["userToken" => $userToken]);

        if ($note == NULL || $note['stauts'] == '0' || $note['status'] == '3') {
            $certifyInfoStore = new CertifyInfoStoreModel($_POST);
            $message .= $certifyInfoStore->allowField(true)->saveAll($basicInfo) ? 'save basic_info success.' : 'save basic_info fail.';
            $message .= $certifyInfoStore->allowField(true)->saveAll($expertInfo) ? 'save expert_info success.' : 'save expert_info fail.';
            $message .= $certifyInfoStore->allowField(true)->saveAll($photoInfo) ? 'save photo_info success.' : 'save photo_info faile.';
            //存放认证的数据后，需要新增或者修改状态表的数据
            if ($note == NULL) {
                //不存在，新增
                $newStatus = new CertifyStatusModel;
                $newStatus->userToken = $userToken;
                $newStatus->status = '2';
                $message .= $newStatus->isUpdate(false)->save() ? "save status success." : "save status fail.";
            }
            else {
                //存在，修改
                $note["status"] = '2';
                $message = $note->isUpdate(true)->save() ? "update status success." : "update status fail.";               
            }
        }
        else {
            $message .= "You do not have seniority to certify expert.";
        }


        $JsonData = controller("index/JsonData");
        $json = $JsonData->getJsonData(0, $message, array());
        return $json;

    }
    public function certifyStatus($userToken)
    {
       
        $status = 0;
        $note = CertifyStatusModel::get(["userToken" => $userToken]);
        if ($status == NULL) {
            $status = 0;
        }
        else {
            $status = (int)$note["status"];
        }
        return $note["status"];
    }
}
